<?php
/**
 * RoomController
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */

namespace Api\Controller;

use Admin\Service\RoomService;
use Model\LiveCateogyModel;
use Model\LiveUrlModel;
use Model\UserFavoriteModel;
use Model\UserModel;
use Think\Controller;
use Model\RoomModel;

class RoomController extends BaseController
{
    /**
     * 直播间列表
     * @http-method GET
     */
    public function find()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $where = 'status = 1 ';
        $keyword = I('get.keyword');
        if (!empty($keyword)) {
            if(is_numeric($keyword) == 1){
                $where = 'roomNumber like "%'.$keyword.'%"';
            }else{
                $where = 'nickname like "%'.$keyword.'%"';
            }
        }

        $roomModel = new RoomModel();
        $roomList = $roomModel->where($where)->select();
        $userModel = new UserModel();
        $roomService = new RoomService();

        //获取直播间在线人数
        $roomNums = $roomService->getRoomNums();
        foreach($roomList as $k=>$v){
            $userInfo = $userModel->get($v['userId']);
            if($userInfo['from'] == 0){
                $roomList[$k]['picture'] = $_SERVER["SERVER_NAME"].$userInfo['picture'];
            }else{
                $roomList[$k]['picture'] = $userInfo['picture'];
            }
            $roomList[$k]['image'] = $_SERVER["SERVER_NAME"].'/'.$v['image'];

            $roomList[$k]['nums'] = 0;
            foreach($roomNums as $roomNum){
                if($v['id'] == $roomNum['roomId']){
                    $roomList[$k]['nums'] = $roomNum['roomNums'];
                }
            }
        }

        $result = [];
        if($roomList){
            $result['code'] = 200;
            $result['msg'] = $roomList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }

        $this->ajaxReturn($result, 'JSON');
    }

    /**
     * 获取某一直播间
     * @http-method GET
     */
    public function get()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $userId = '';
        $username = '';
        $type = trim(I('post.type'));
        $roomId = I('post.roomId');
        $roomNumber = I('post.roomNumber');
        $result = [];
        if(empty($roomNumber) || empty($roomId) || empty($type)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        if($type != 'connect'){
            $userId = I('post.userId');
            $username = I('post.userName');
            if(empty($userId) || empty($username)){
                $result['code'] = 404;
                $result['msg'] = 'userId, username 参数异常';

                $this->ajaxReturn($result, 'JSON');
            }
        }
        $systemToken = 'icesmart.cn';
        $token = md5($roomId.$userId.$username.$systemToken);

        $roomModel = new RoomModel();
        $roomInfo = $roomModel->get($roomId);

        if($roomInfo){
            $roomInfo['token'] = $token;
            $roomInfo['image'] = $_SERVER["SERVER_NAME"].'/'.$roomInfo['image'];
            $roomInfo['socketServerIp'] = C('SOCKET_SERVER_IP');
            $roomInfo['socketServerport'] = C('SOCKET_SERVER_PORT');

            //获取直播间人数
            $roomService = new RoomService();
            $roomNums = $roomService->getRoomNums();
            $roomInfo['nums'] = 0;
            foreach($roomNums as $roomNum){
                if($roomInfo['id'] == $roomNum['roomId']){
                    $roomInfo['nums'] = $roomNum['roomNums'];
                }
            }

            $liveUrlModel = new LiveUrlModel();
            $LiveUrlInfo = $liveUrlModel->where('roomId = '.$roomInfo['id'])->find();
            if($LiveUrlInfo){
                $roomInfo['play_m3u8'] = $LiveUrlInfo['play'].'.m3u8';
                $roomInfo['play_flv'] = $LiveUrlInfo['play'].'.flv';
            }

            $categoryModel = new LiveCateogyModel();
            $categoryInfo = $categoryModel->get($roomInfo['categoryId']);
            $roomInfo['categoryName'] = $categoryInfo['name'];

            $userModel = new UserModel();
            $userInfo = $userModel->get($roomInfo['userId']);
            if($userInfo['from'] == 0){
                $roomInfo['picture'] = $_SERVER["SERVER_NAME"].$userInfo['picture'];
            }else{
                $roomInfo['picture'] = $userInfo['picture'];
            }
            $roomInfo['username'] = $userInfo['nickname'];

            //获取该直播间关注数
            $userFavModel = new UserFavoriteModel();
            $userFavCount = $userFavModel->countByFilter(['roomId' => $roomInfo['id']]);
            $roomInfo['favorite'] = $userFavCount;

            $result['code'] = 200;
            $result['msg'] = $roomInfo;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }

        $this->ajaxReturn($result, 'JSON');
    }
} 